/**
 * GameView.java
 * @author: liuex
 * 2008-1-12 ����02:28:34
 *
 * note: 
 */
package lex.lianliankan.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import org.lex.swing.Swings;

import lex.lianliankan.GameModel;
import lex.lianliankan.LianLianKanListener;
import lex.lianliankan.utils.IconProvider;
import lex.lianliankan.utils.Images;
import lex.lianliankan.utils.ModelUtils;
import lex.lianliankan.utils.ModelUtils.Cell;

public class GameView extends JPanel implements LianLianKanListener, ActionListener
{
	private GameModel model = null;
	private CellRenderer[][] renderers = null;
	private Cell last = null;
	// =====================================
	private IconProvider iconProvider = null;

	public static void main( String[] args )
	{
		GameModel model = new GameModel();
		model.create();
		ModelUtils.dump( model );
		GameView view = new GameView( Images.getInstance() );
		view.setModel( model );
		Swings.show( view, false );
	}

	public GameView( IconProvider ip )
	{
		this.iconProvider = ip;
	}

	public void setModel( GameModel model )
	{
		if( null != this.model )
			this.model.removeLianLianKanListener( this );
		// remove previous cells
		if( null != this.renderers )
		{
			for( int r = 0; r < renderers.length; r++ )
				for( int c = 0; c < renderers[r].length; c++ )
					renderers[r][c].removeActionListener( this );
		}
		this.removeAll();
		// create CellRenderers from model
		this.setLayout( new GridLayout( model.getRowCount(), model.getColumnCount(), 2, 2 ) );
		this.renderers = new CellRenderer[model.getRowCount()][model.getColumnCount()];
		for( int r = 0; r < renderers.length; r++ )
			for( int c = 0; c < renderers[r].length; c++ )
			{
				CellRenderer cr = new CellRenderer( r, c );
				cr.addActionListener( this );
				int key = model.getKey( r, c );
				if( 0 != key )
					cr.setIcon( this.iconProvider.getIcon( key ) );
				this.add( cr );
				renderers[r][c] = cr;
			}
		this.model = model;
		this.model.addLianLianKanListener( this );
	}

	@Override
	public void actionPerformed( ActionEvent e )
	{
		CellRenderer cr = (CellRenderer) e.getSource();
		if( false == cr.isSelected() )
			return;
		Cell cur = new Cell( cr.getRow(), cr.getColumn() );
		if( null == this.last )
		{
			this.last = cur;
		}
		else
		{
			if( model.canRemove( last.row, last.col, cur.row, cur.col ) )
			{
				model.setKey( 0, last.row, last.col );
				model.setKey( 0, cur.row, cur.col );
				System.out.println( "remove: " + last + " " + cur );
			}
			else
			{
				renderers[last.row][last.col].setSelected( false );
				renderers[cur.row][cur.col].setSelected( false );
			}
			last = null;
		}
	}

	@Override
	public void cellChanged( int row, int column )
	{
		int key = model.getKey( row, column );
		CellRenderer cr = renderers[row][column];
		if( 0 == key )
		{
			cr.setSelected( false );
			cr.setVisible( false );
		}
		else
		{
			cr.setIcon( iconProvider.getIcon( key ) );
		}
	}
}
